package cn.wolfcode.service.impl;

import cn.wolfcode.anno.RequiredPermission;
import cn.wolfcode.domain.Permission;
import cn.wolfcode.mapper.PermissionMapper;
import cn.wolfcode.query.PageResult;
import cn.wolfcode.query.QueryObject;
import cn.wolfcode.service.IPermissionService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.lang.reflect.Method;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Map;

@Service
@Transactional
public class PermissionServiceImpl implements IPermissionService {
    @Autowired
    private PermissionMapper permissionMapper;
    @Override
    public void insert(Permission permission) {
        permissionMapper.insert(permission);
    }

    @Override
    public void delete(Long id) {
        permissionMapper.deleteByPrimaryKey(id);
    }

    @Override
    public void update(Permission permission) {
        permissionMapper.updateByPrimaryKey(permission);
    }

    @Override
    public Permission selectOne(Long id) {
        Permission permission = permissionMapper.selectByPrimaryKey(id);
        return permission;
    }

    @Override
    public List<Permission> selectAll() {
        List<Permission> list = permissionMapper.selectAll();
        return list;
    }

    @Override
    public PageInfo<Permission> query(QueryObject queryObject) {
        PageHelper.startPage(queryObject.getCurrentPage(), queryObject.getPageSize());
        List<Permission> list = permissionMapper.queryForList(queryObject);
        return new PageInfo(list);
    }

    @Autowired
    private ApplicationContext ctx;//Spring上下文对象

    @Override
    public void reload() {
        //获取所有的权限表达式
        List<String> expressions = permissionMapper.selectAllExpression();
        //1、通过注解获取到所有的控制器(从Spring容器中拿)
        Map<String, Object> map = ctx.getBeansWithAnnotation(Controller.class);
        Collection<Object> controllers = map.values();//获取控制器
        //2、获取每个控制器中的方法(遍历)
        for(Object controller:controllers){
            //获取控制器字节码对象
            Class<?> clazz = controller.getClass();
            //3、遍历获取到每个方法
            Method[] methods = clazz.getDeclaredMethods();
            //遍历每个方法，并判断方法上是否贴有注解
            for(Method m:methods){
                //4、判断是否有贴自定义注解
                RequiredPermission annotation = m.getAnnotation(RequiredPermission.class);

                //5、从注解中获取权限相关的数据，并封装成权限对象
                //判断annotation是否为空
                if(annotation!=null){
                    String name = annotation.name();
                    String expression = annotation.expression();

                    if(!expressions.contains(expression)){
                        //6、封装成权限对象，插入到数据库当中
                        Permission permission = new Permission();
                        permission.setName(name);
                        permission.setExpression(expression);
                        //将对象保存到数据库中
                        permissionMapper.insert(permission);
                    }
                }
            }
        }
    }

    @Override
    public List<String> selectAllPermissions(Long id) {
        List<String> list =permissionMapper.selectAllPermissions(id);
        return list;
    }
}
